<template>
  <div class="article-list" ref="article-list">
    <van-pull-refresh
      v-model="refreshing"
      :success-text="refreshSuccessText"
      :success-duration="1500"
      @refresh="onRefresh"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <article-item
          v-for="(article, index) in articles"
          :key="index"
          :article="article"
        />
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticles } from '@/api/article'
import ArticleItem from '@/components/article.item'
import { debounce } from 'lodash'

export default {
  name: 'ArticleList',
  components: {
    ArticleItem
  },
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      articles: [], // 数据列表
      loading: false, // 控制加载中的 loading 状态
      finished: false, // 控制加载结束的状态，当加载结束，不再触发加载更多
      timestamp: 1556789000001, // 获取下一页数据的时间戳
      error: false, // 控制加载
      refreshing: false, // 控制下拉刷新的 loading 状态
      refreshSuccessText: '',
      scrollTop: 0
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    const articleList = this.$refs['article-list']
    articleList.onscroll = debounce(() => {
      this.scrollTop = articleList.scrollTop
    }, 30)
  },
  activated() {
    this.$refs['article-list'].scrollTop = this.scrollTop
  },
  methods: {
    async onLoad() {
      try {
        // 1. 请求获取数据
        const { data } = await getArticles({
          channel_id: this.channel.id, // 频道 ID
          timestamp: this.timestamp,
          with_top: 1
        })

        // 模拟随即失败的情况
        // if (Math.random() > 0.5) {
        //   JSON.parse('asdasdasda')
        // }

        // 2. 把数据放到 list 数组中
        const { results } = data.data
        this.articles.push(...results)

        // 3. 设置本次加载状态结束，它才可以判断是否需要加载下一次，否则就会永远的停在这里
        this.loading = false

        // 4. 数据全部加载完成
        if (results.length) {
          // 更新获取下一页数据的页码
          this.timestamp = data.data.pre_timestamp
        } else {
          // 没有数据了，把加载状态设置结束，不再触发加载更多
          this.finished = true
        }
      } catch (error) {
        // 展示错误提示状态
        this.error = true
        // 请求失败，loading 也需要关闭
        this.loading = false
      }
    },
    async onRefresh() {
      try {
        // 请求获取数据
        const { data } = await getArticles({
          channel_id: this.channel.id, // 频道 ID
          timestamp: this.timestamp,
          with_top: 1
        })

        // 模拟随即失败的情况
        // if (Math.random() > 0.5) {
        //   JSON.parse('asdasdasda')
        // }

        // 将数据追加到列表的顶部
        const { results } = data.data
        this.articles.unshift(...results)
        // 关闭下拉刷新 loading 状态
        this.refreshing = false
        // 更新下拉刷新成功提示的文本
        this.refreshSuccessText = `刷新成功，更新了${results.length}条数据`
      } catch (error) {
        // 更新下拉刷新成功提示的文本
        this.refreshSuccessText = '刷新失败！'
        this.refreshing = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.article-list {
  height: 79vh;
  overflow-y: auto;
}
</style>
